package com.sq.goodline.service;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sq.goodline.config.JobConfig;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class RemoveFileService {
	
	@Autowired
	JobConfig jobConfig;
	 
	
	/**
	 * 删除temp目录的临时文件
	 */
	public void removeTempFile() {
		try {
			File tempFile = new File(jobConfig.getRemoveDirectory());
			File[] files = tempFile.listFiles();
			if(files==null || files.length<1) {
				log.info("无待删除文件");
				return;
			}
			int a = 0;
			long timeLimit=24*60*60*1000*jobConfig.getFileSaveDays();
			for(File ff:files) {
				long createTime = ff.lastModified();
				if(System.currentTimeMillis()-createTime>timeLimit) {//超过指定天数，删除文件
					boolean d = ff.delete();
					log.info("删除文件：{}，{}",ff.getPath(),d);
					a++;
				}
			}
			log.info("共删除文件数：{}",a);
		}catch(Exception e) {
			log.error("删除temp目录文件异常：",e);
		}
	}

	 
}
